package hankz.spring.core.io;

import java.net.MalformedURLException;
import java.net.URL;
import org.springframework.util.Assert;

/**
 * @description: TODO
 * @author: zzc
 * @modified By: zzc
 * @date: Created in 2021/11/21 15:26
 * @version:v1.0
 */
public class DefaultResourceLoader implements ResourceLoader{

    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "location must not be null");
        if (location.startsWith(CLASSPATH_UTL_PREFIX)) {
            return new ClassPathResource(location.substring(CLASSPATH_UTL_PREFIX.length()));
        }
        else {
            try {
                URL url = new URL(location);
                return new UrlResource(url);
            } catch (MalformedURLException e) {
                return new FileSystemResource(location);
            }
        }
    }
}
